package com.k.ums.controller;

import com.alibaba.fastjson.JSON;
import com.k.ums.model.SysUser;
import com.k.ums.service.SysUserService;
import com.k.ums.util.CacheUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: keen
 * Date: 2019-04-06
 * Time: 17:41
 */

@Controller
public class HelloController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/hello")
    public String hello(Map<String, Object> map) {
        map.put("hello", "你好");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", "hello");
        map.put("map", hashMap);
        return "hello";
    }

    @RequestMapping("/testRedis")
    public String testRedis() {
        int userId = 2;
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setUserName("TestRedis6666");
        sysUser.setAge(3);
        sysUser.setGender("1");
        sysUser.setPhone("666666");
        sysUser.setAddress("中国");
        sysUser.setEmail("test@test");
        sysUser.setQq("666666");
        sysUser.setWeixin("wx666666");
        sysUser.setLastUpdatedBy("System");
        sysUser.setRemark("Test Redis");
        sysUser.setBirthDate(new Date());
        sysUser.setPassword(passwordEncoder.encode("123456"));
        String key = "user_id_" + userId;
        Thread writeThread = new Thread(() -> {
            System.out.println("==========write thread 11111111===========");
            CacheUtils.delete(key);
            try {
                TimeUnit.MILLISECONDS.sleep(2000);
            } catch (InterruptedException e) {

            }
            System.out.println("==========write thread 2222222===========");
            sysUserService.update(sysUser);
            CacheUtils.deleteAsync(key, 3000);
        });
        Thread readThread = new Thread(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(300);
            } catch (InterruptedException e) {

            }
            System.out.println("==========read thread 11111111111===========");
            String user = CacheUtils.get(key);
            if (StringUtils.isBlank(user)) {
                System.out.println("==========cache is null===========");
                user = JSON.toJSONString(sysUserService.selectById(userId));
                CacheUtils.set(key, user);
                System.out.println("==========read thread 2222222===========");
            }
            System.out.println(user);
        });
        writeThread.start();
        readThread.start();
        return "success";
    }
}
